﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace PloobsEngine.SceneControl
{
    public class ToonPostEffect : IPostEffect
    {
        #region IPostEffect Members

        private Effect toon;
        private Effect line;
        private Texture2D cell;
        private Vector2 halfPixel;
        private SpriteBatch spriteBatch;
        private EngineStuff engine;
        private RenderTarget2D target;
        private bool useLight = true;
        private bool changeColors= true;

        public bool UseLight
        {
            set
            {
                this.useLight = value;
            }
            get
            {
                return this.useLight;
            }
        }

        public bool ChangeColors
        {
            set
            {
                this.changeColors = value;
            }
            get
            {
                return this.changeColors;
            }
        }


        public void init(EngineStuff engine)
        {
            this.toon = EngineStuff.InternalContentManager.GetAsset<Effect>("toon1");
            this.line = EngineStuff.InternalContentManager.GetAsset<Effect>("LineDetection");
            this.cell= EngineStuff.InternalContentManager.GetAsset<Texture2D>("Toon");
            halfPixel.X = 0.5f / (float)EngineStuff.GraphicsDevice.PresentationParameters.BackBufferWidth;
            halfPixel.Y = 0.5f / (float)EngineStuff.GraphicsDevice.PresentationParameters.BackBufferHeight;
            spriteBatch = EngineStuff.SpriteBatch;
            target = EngineStuff.GetDefaultColorBuffer();
            this.engine = engine;
            
        }

        public void Draw(IRenderHelper rHelper, Microsoft.Xna.Framework.GameTime gt, Microsoft.Xna.Framework.Rectangle screenRec, IWorld world)
        {
            if (changeColors)
            {
                RenderTarget2D rd = EngineStuff.GraphicsDevice.GetRenderTarget(0) as RenderTarget2D;
                EngineStuff.GraphicsDevice.SetRenderTarget(0, target);
                EngineStuff.GraphicsDevice.Clear(engine.BackGroundColor);

                    toon.Parameters["ColorMap"].SetValue(rHelper[PrincipalConstants.CombinedImage]);
                    toon.Parameters["useLight"].SetValue(useLight);
                    toon.Parameters["TexColor"].SetValue(rHelper[PrincipalConstants.colorRT]);
                    toon.Parameters["CelMap"].SetValue(cell);
                    toon.Parameters["LightMap"].SetValue(rHelper[PrincipalConstants.lightRt]);
                    toon.Parameters["halfPixel"].SetValue(halfPixel);
                    rHelper.RenderTextureToFullScreenVertexPixel(toon);
               
              
                EngineStuff.GraphicsDevice.SetRenderTarget(0, rd);
                rHelper[PrincipalConstants.CurrentImage] = target.GetTexture();
            }

            spriteBatch.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.SaveState);
            {                
                line.Begin();
                {
                    line.CurrentTechnique.Passes[0].Begin();
                    {
                        line.Parameters["Thickness"].SetValue(1.0f);
                        line.Parameters["Threshold"].SetValue(0.2f);
                        spriteBatch.Draw(rHelper[PrincipalConstants.CurrentImage],screenRec, Color.White);
                        line.CurrentTechnique.Passes[0].End();
                    }
                }
                line.End();
            }
            spriteBatch.End();
            



        }

        #endregion
    }
}
